﻿// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Licensed under the Microsoft Public License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//  
// http://opensource.org/licenses/ms-pl
//  
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -----------------------------------------------------------------------


using Microsoft.VisualStudio.SharePoint;
using System;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;

namespace Microsoft.Samples.VisualStudio.SharePoint
{
    [Export(typeof(ISharePointProjectItemTypeExtension))]
    [SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.Workflow4CustomActivity")]
    internal class CustomActivitySPIExtension : ISharePointProjectItemTypeExtension
    {
        const string OldProjectItemType = "Microsoft.VisualStudio.SharePoint.Workflow4CustomActivity";
        const string NewProjectItemType = "Microsoft.VisualStudio.SharePoint.Workflow4AppCustomActivity";

        public const string Namespace = "http://schemas.microsoft.com/sharepoint/";

        public void Initialize(ISharePointProjectItemType projectItemType)
        {
            projectItemType.ProjectItemMenuItemsRequested += projectItemType_ProjectItemMenuItemsRequested;
        }

        private void projectItemType_ProjectItemMenuItemsRequested(object sender, SharePointProjectItemMenuItemsRequestedEventArgs e)
        {
            e.ViewMenuItems.Add("Convert to App-style Custom Activity").Click += CustomActivitySPIUpgrade_Click;
            e.ViewMenuItems.Add("Update Custom Activity references").Click += CustomActivityReferencesUpdate_Click;
        }

        private void CustomActivityReferencesUpdate_Click(object sender, MenuItemEventArgs e)
        {
            ISharePointProjectItem toConvertProjectItem = (ISharePointProjectItem)e.Owner;

            if (toConvertProjectItem == null || toConvertProjectItem.Project == null)
            {
                return;
            }

            Utilities.UpdateProjectItemReferences(toConvertProjectItem, "Could not find Custom Activity file for this Custom Activity");
        }

        private void CustomActivitySPIUpgrade_Click(object sender, MenuItemEventArgs e)
        {
            ISharePointProjectItem toConvertProjectItem = (ISharePointProjectItem)e.Owner;

            if (toConvertProjectItem == null || toConvertProjectItem.Project == null)
            {
                return;
            }

            var projectService = toConvertProjectItem.Project.ProjectService;

            var newProjectItem = Utilities.CreateNewSpi(toConvertProjectItem.Project, toConvertProjectItem.Name, NewProjectItemType);

            if (newProjectItem == null)
            {
                projectService.Logger.WriteLine(
                    String.Format("Unable to create a sharepoint project item with name {0} in project {1}.",
                    toConvertProjectItem.Name, toConvertProjectItem.Project.Name), LogCategory.Error);
                return;
            }

            var xamlFile = toConvertProjectItem.Files
                .Where((file) => StringComparer.OrdinalIgnoreCase.Equals(Path.GetExtension(file.FullPath), ".xaml"))
                .FirstOrDefault();

            if (xamlFile == null)
            {
                projectService.Logger.WriteLine("Could not find Custom Activity file for this Custom Activity", LogCategory.Error);
                return;
            }

            var buildActon = projectService.Convert<ISharePointProjectItemFile, EnvDTE.ProjectItem>(xamlFile).GetBuildAction();

            Utilities.CopyFiles(toConvertProjectItem, projectService, newProjectItem, buildActon);
            newProjectItem.Project.Synchronize();
        }
    }
}
